#include "tabwin1.h"
#include "ui_tabwin1.h"
#include "qtimshow.h"
#include "ui_qtimshow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

TabWin1::TabWin1(QWidget *parent)
    :ui(new Ui::TabWin1)
{
    ui->setupUi(this);
    mainTabWidget = static_cast<QTabWidget*>(parent);
    mainTabWidget->addTab(this, "主窗口");
    listModel = new QStandardItemModel();
    ui->listView->setModel(listModel);
    init1();
    init2();
    connect(ui->lineEdit, &MainEdit::Slots_Click, this, &TabWin1::on_mainEdit_click);
    connect(&colorDialog, &QColorDialog::colorSelected, [=](const QColor &color){
        if(!color.isValid()) return;
        int r,g,b,h,s,v;
        color.getRgb(&r,&g,&b);
        color.getHsv(&h,&s,&v);
        ui->horizontalSlider->setValue(h);
        ui->spinBox->setValue(h);
        ui->horizontalSlider_2->setValue(s);
        ui->spinBox_2->setValue(s);
        ui->horizontalSlider_3->setValue(v);
        ui->spinBox_3->setValue(v);
        ui->groupBox->setStyleSheet(QString("QGroupBox::indicator { background-color: rgb(%1, %2, %3); }").arg(r).arg(g).arg(b));
    });
    connect(&colorDialog2, &QColorDialog::colorSelected, [=](const QColor &color){
        if(!color.isValid()) return;
        int r,g,b,h,s,v;
        color.getRgb(&r,&g,&b);
        color.getHsv(&h,&s,&v);
        ui->horizontalSlider_13->setValue(h);
        ui->spinBox_13->setValue(h);
        ui->horizontalSlider_14->setValue(s);
        ui->spinBox_14->setValue(s);
        ui->horizontalSlider_15->setValue(v);
        ui->spinBox_15->setValue(v);
        ui->groupBox_2->setStyleSheet(QString("QGroupBox::indicator { background-color: rgb(%1, %2, %3); }").arg(r).arg(g).arg(b));
    });

}

TabWin1::~TabWin1()
{
    delete ui;
}

void TabWin1::on_spinBox_valueChanged(int arg1)
{
    ui->horizontalSlider->setValue(arg1);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}

void TabWin1::on_horizontalSlider_valueChanged(int value)
{
    ui->spinBox->setValue(value);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_horizontalSlider_2_valueChanged(int value)
{
    ui->spinBox_2->setValue(value);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_spinBox_2_valueChanged(int arg1)
{
    ui->horizontalSlider_2->setValue(arg1);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_horizontalSlider_3_valueChanged(int value)
{
    ui->spinBox_3->setValue(value);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_spinBox_3_valueChanged(int arg1)
{
    ui->horizontalSlider_3->setValue(arg1);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_horizontalSlider_13_valueChanged(int value)
{
    ui->spinBox_13->setValue(value);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_spinBox_13_valueChanged(int arg1)
{
    ui->horizontalSlider_13->setValue(arg1);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_horizontalSlider_14_valueChanged(int value)
{
    ui->spinBox_14->setValue(value);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_spinBox_14_valueChanged(int arg1)
{
    ui->horizontalSlider_14->setValue(arg1);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_horizontalSlider_15_valueChanged(int value)
{
    ui->spinBox_15->setValue(value);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_spinBox_15_valueChanged(int arg1)
{
    ui->horizontalSlider_15->setValue(arg1);
    HSV2RGBButton();
    if(0==listModel->rowCount()) opencv_test();
}


void TabWin1::on_horizontalSlider_16_valueChanged(int value)
{
    ui->groupBox_3->setTitle("Zoom:"+QString::number(value));
    opencv_test();
}




// lineEdit双击打开image
void TabWin1::on_mainEdit_click()
{
    QString selectedFile = QFileDialog::getOpenFileName(this, "Open File", QDir::homePath(), "image files (*);");
    if(!selectedFile.isEmpty()){
        ui->lineEdit->setText(selectedFile);
        QImage image(selectedFile);
        if(!image.isNull()){
            srcImage = QImage2cvMat(image);
            opencv_test();
        }else{
            ui->lineEdit->setText(selectedFile+"-打开失败");
        }
        //srcImage = cv::imread(selectedFile.toStdString());
    }else{
        ui->lineEdit->setText("");
    }
}




void TabWin1::HSV2RGBButton(){
    QColor color;
    color.setHsv(ui->horizontalSlider->value(), ui->horizontalSlider_2->value(), ui->horizontalSlider_3->value());
    int r,g,b;
    color.getRgb(&r,&g,&b);
    ui->groupBox->setStyleSheet(QString("QGroupBox::indicator { background-color: rgb(%1, %2, %3); }").arg(r).arg(g).arg(b));
    color.setHsv(ui->horizontalSlider_13->value(), ui->horizontalSlider_14->value(), ui->horizontalSlider_15->value());
    color.getRgb(&r,&g,&b);
    ui->groupBox_2->setStyleSheet(QString("QGroupBox::indicator { background-color: rgb(%1, %2, %3); }").arg(r).arg(g).arg(b));
}

void TabWin1::showImage(QString wintitle, cv::Mat &mat){
#ifndef WIN32
    // TAB WIDGET on Android
    // 检查图像是否已经是BGR格式
    if(mat.empty()) return;
    QImage qImage=cvMat2QImage(mat);
    if(qImage.isNull()) return;
    QtImShow *show=nullptr;
    for (int var = 0; var < mainTabWidget->tabBar()->count(); ++var) {
        if(mainTabWidget->tabText(var)==wintitle){
            show = static_cast<QtImShow*>(mainTabWidget->widget(var));
        }
    }
    if(show==nullptr) {
        show = new QtImShow();
        mainTabWidget->insertTab(1, show, wintitle);
    }
    show->getLabel()->setPixmap(QPixmap::fromImage(qImage));
#else
    // OPENCV on windows
    open_cv_windows.insert(wintitle);
    cv::namedWindow(wintitle.toStdString(), cv::WINDOW_AUTOSIZE);
    cv::imshow(wintitle.toStdString(), mat);
#endif
}

void TabWin1::on_tabWidget_currentChanged(int index)
{
    if(index==0)
        timer->start();
}


void TabWin1::on_pushButton_5_clicked()
{
    ui->tableView->customContextMenuRequested(ui->tableView->pos());
}


void TabWin1::on_pushButton_4_clicked()
{
    ui->tableView_2->customContextMenuRequested(ui->tableView->pos());
}


void TabWin1::on_groupBox_clicked()
{
    ui->groupBox->setChecked(1);
    QColor color;
    color.setHsv(ui->horizontalSlider->value(), ui->horizontalSlider_2->value(), ui->horizontalSlider_3->value());
    colorDialog.setCurrentColor(color);
    colorDialog.activateWindow();
    colorDialog.show();
}

void TabWin1::on_groupBox_2_clicked()
{
    ui->groupBox_2->setChecked(1);
    QColor color;
    color.setHsv(ui->horizontalSlider_13->value(), ui->horizontalSlider_14->value(), ui->horizontalSlider_15->value());
    colorDialog2.setCurrentColor(color);
    colorDialog2.activateWindow();
    colorDialog2.show();
}

// 导入特征文件的阈值范围
void TabWin1::on_pushButton_2_clicked()
{
    QString selectedFile = QFileDialog::getOpenFileName(this, "Open File", QDir::homePath(), "contoursDataFiles (*.txt);");
    if(!selectedFile.isEmpty()){
        QFile file(selectedFile);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            //ui->lineEdit_2->setText(selectedFile+"-打开失败");
            return;
        }
        QTextStream in(&file);
        QString line;
        // 获取第一行
        if(!in.atEnd()){
            line = in.readLine();
            QList<QString> strList = line.split(";");
            if(line.size()<6){
                //ui->lineEdit_2->setText("文件格式错误."+selectedFile);
                file.close();
                return;
            }
            ui->spinBox->setValue(strList[0].toInt());
            ui->horizontalSlider->setValue(strList[0].toInt());
            ui->spinBox_2->setValue(strList[1].toInt());
            ui->horizontalSlider_2->setValue(strList[1].toInt());
            ui->spinBox_3->setValue(strList[2].toInt());
            ui->horizontalSlider_3->setValue(strList[2].toInt());
            ui->spinBox_13->setValue(strList[3].toInt());
            ui->horizontalSlider_13->setValue(strList[3].toInt());
            ui->spinBox_14->setValue(strList[4].toInt());
            ui->horizontalSlider_14->setValue(strList[4].toInt());
            ui->spinBox_15->setValue(strList[5].toInt());
            ui->horizontalSlider_15->setValue(strList[5].toInt());
            on_pushButton_6_clicked();
        }
        file.close();
    }
}

